package com.iamteer.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.iamteer.dao.IDinnerTableDao;
import com.iamteer.entity.DinnerTable;
import com.iamteer.utils.JdbcUtils;
/**
 * 2. 餐桌 dao 实现
 * @author Chuck
 *
 */
public class DinnerTableDao implements IDinnerTableDao {
	private QueryRunner qr = JdbcUtils.getQueryRunner();
	
	@Override
	public void add(DinnerTable dt) {
		try {
			String sql = "insert into dinnerTable(tName) values(?)";
			qr.update(sql,dt.gettName());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void delete(int id) {
		try {
			String sql = "delete from dinnerTable where id=?";
			qr.update(sql,id);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}		
	}

	@Override
	public void update(DinnerTable dt) {
		try {
			String sql = "update dinnerTable set tName=?,tStatus=?,bookTime=? where id=?";
			qr.update(sql, dt.gettName(), dt.gettStatus(), dt.getBookTime(), dt.getId());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}	
	}

	@Override
	public List<DinnerTable> query() {
		try {
			String sql = "select * from dinnerTable";
			return qr.query(sql, new BeanListHandler<DinnerTable>(DinnerTable.class));
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<DinnerTable> query(int tStatus) {
		try {
			String sql = "select * from dinnerTable where tStatus=?";
			return qr.query(sql, new BeanListHandler<DinnerTable>(DinnerTable.class), tStatus);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	

	@Override
	public DinnerTable findById(int id) {
		try {
			String sql = "select * from dinnerTable where id=?";
			return qr.query(sql, new BeanHandler<DinnerTable>(DinnerTable.class), id);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

}
